#include"StackQueue.h"
void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void AddCapacity(Stack * ps)
{
	if (ps->top == ps->capacity)
	{
		int NewCapacity = ps->a == NULL ? 4 : ps->capacity * 2;
		// ps->a == NUll realloc 等价于 malloc
		STDataType* New = (STDataType*)realloc(ps->a, sizeof(STDataType) * NewCapacity);
		assert(New);
		ps->a = New;
		ps->capacity = NewCapacity;
	}
}

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);

	// 先判断增容
	AddCapacity(ps);

	// 添加数据
	ps->a[ps->top] = x;
	ps->top++;
}

void StackDestory(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void StackPop(Stack* ps) // 出栈
{
	assert(ps);
	// 栈为空
	assert(!StackEmpty(ps));

	ps->top--;
}

void StackPrint(Stack* ps) // 随便写的
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->top; i++)
	{
		printf("%d---", ps->a[i]);
	}
	printf("END\n");
}

STDataType StackTop(Stack* ps)  // 取栈顶的数据
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

bool StackEmpty(Stack* ps) 
{
	assert(ps);

	//if (ps->top == 0)
	//	return true;
	//else
	//	return false;
	return ps->top == 0;
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}